<template>
  <el-dialog
    title="编辑模块"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="30%"
  >
    <div class="form">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-position="left"
        label-width="80"
        size="small"
      >
        <quick-field-panel title="模块名称" required>
          <el-form-item prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入模块名称"
              clearable
            >
            </el-input>
          </el-form-item>
        </quick-field-panel>
        <quick-field-panel title="详细说明" required>
          <el-form-item prop="remark">
            <el-input
              type="textarea"
              v-model="form.remark"
              placeholder="详细说明"
              :rows="4"
              clearable
              maxlength="200"
              show-word-limit
            >
            </el-input>
          </el-form-item>
        </quick-field-panel>
      </el-form>
    </div>
    <span slot="footer">
      <el-button type="primary" :loading="loading" size="medium" @click="doEdit"
        >确 定</el-button
      >
      <el-button size="medium" plain @click="isShow = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import _api from "../../common/api/project_module";
import _project from "../../common/api/project";

export default {
  data() {
    return {
      loading: false,
      isShow: false,
      form: {
        name: "",
        id: "",
        remark: "",
      },

      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        remark: [
          { required: true, message: "必填", trigger: "blur" },
          {
            min: 10,
            message: "详细说明十字以上",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    showDialog(module) {
      this.form = Object.assign(
        {},
        {
          name: module.name,
          id: module.id,
          remark: module.remark,
        }
      );
      this.form.terminal = +this.form.terminal;
      this.isShow = true;
    },

    doEdit() {
      try {
        this.loading = true;

        this.$refs.form.validate(async (valid) => {
          if (valid) {
            await _api.editAsync(this.form);
            this.$emit("edited", {});
            this.isShow = false;
          }
        });
      } finally {
        this.loading = false;
      }
    },

    close() {
      this.$refs.form.resetFields();
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.avatar-panel {
  display: flex;
  align-items: center;
  justify-content: center;

  margin: 10px 0;
}

.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    &:hover {
      border-color: #409eff;
    }
  }
}

.avatar-uploader .el-upload .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
